var wxJdk = {
    getAccessCode: function(appId, redirectUrl, state) {
        var url = "https://open.weixin.qq.com/connect/oauth2/authorize";
        url += "?appid=" + appId;
        url += "&redirect_uri=" + encodeURIComponent(redirectUrl);
        url += "&response_type=code";
        url += "&scope=snsapi_base";
        url += "&state=" + state + "#wechat_redirect";

        location.href = url;
    },
    pay: function(data) {
        wxJdk.data = JSON.parse(data);

        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', wxJdk.submitPayReq, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', wxJdk.submitPayReq);
                document.attachEvent('onWeixinJSBridgeReady', wxJdk.submitPayReq);
            }
        } else {
            wxJdk.submitPayReq();
        }
    },
    submitPayReq: function() {
        var data = wxJdk.data;

        WeixinJSBridge.invoke('getBrandWCPayRequest', {
                "appId": data.appId,
                "timeStamp": data.timeStamp,
                "nonceStr": data.nonceStr,
                "package": data.pack,
                "signType": data.signType,
                "paySign": data.paySign
            },
            function(res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    location.href = "/qrcodes/success?transNo=" + data.reqId;
                } else {
                    location.href = "/qrcodes/error?transNo=" + data.reqId;
                }
            }
        );
    }
};